import requests
from api.login import Login
import pytest
from api.utils import Utils
import allure
import logging

# logger = logging.getLogger(__name__)

@allure.epic("项目名称：")
@allure.feature('登陆测试')
class Test_api0001_0004:

    data = Utils.yaml_load('case/login_test.yaml')

    # 这个是形态三才用到
    step = Utils.yaml_load('case/login_step.yaml')

    @classmethod
    def setup_class(cls):
        cls.l_obj = Login()
    # @classmethod
    # def setup_class(cls):
    #     cls.data = Utils.yaml_load('login_test.yaml')

    # @pytest.mark.parametrize('username,password,ret,msg',[
    #     ('byhy','88888888',0,None),
    #     ('byhy','8888',1,'用户名或者密码错误'),
    #     (None,'88888888',1,'用户名或者密码错误'),
    #     ('byhy',None,1,'用户名或者密码错误')
    #     ])

    # 第一种形态  测试用例的数据用数据驱动
    @allure.story("登陆测试1")
    @pytest.mark.parametrize('username,password,ret,msg',Utils.yaml_data(data,'test_api_0001_0005'))
    def test_api_0001_0005(self,username,password,ret,msg):
        # data = {
        #     'username':username,
        #     'password':password
        # }
        # response = requests.post('http://127.0.0.1/api/mgr/signin',data=data) 
        response = self.l_obj.login_requests(username,password)
        rs_code = response.status_code
        rs_body = response.json()
        rs_ret = rs_body['ret']
        if rs_ret:
            rs_msg = rs_body['msg']
        else:
            rs_msg = None
        print(f'在cases里面：rs_code:{rs_code},rs_ret:{rs_ret},rs_msg:{rs_msg}')

        
        # logger.info(f'在cases里面：rs_code:{rs_code},rs_ret:{rs_ret},rs_msg:{rs_msg}')

        assert rs_code == 200
        assert rs_ret == ret
        assert str(rs_msg) == msg
    
    # 第二种形态，api用了测试步骤数据驱动， 测试用例用了数据 的数据驱动
    @allure.story('登陆测试-分离步骤')
    @pytest.mark.parametrize('username,password,ret,msg',Utils.yaml_data(data,'test_api_0001_0005'))
    def test_login_api(self,username,password,ret,msg):
        # data = {
        #     'username':username,
        #     'password':password
        # }
        # response = requests.post('http://127.0.0.1/api/mgr/signin',data=data) 
        response = self.l_obj.login_requests_api(username,password)
        rs_code = response.status_code
        rs_body = response.json()
        rs_ret = rs_body['ret']
        if rs_ret:
            rs_msg = rs_body['msg']
        else:
            rs_msg = None
        print(f'在cases里面：rs_code:{rs_code},rs_ret:{rs_ret},rs_msg:{rs_msg}')
        assert rs_code == 200
        assert rs_ret == ret
        assert str(rs_msg) == msg

    # 第三种形态，测试用例用到了步骤和数据 的两种数据驱动   ，api用到了 步骤的数据驱动
    def test_api_yaml(self):
        self.l_obj.steps_run(self.step['Test_api_yaml'])








# 下面的两个类不重要
@allure.story('登陆测试2')
class Test_api_0005:

    def test_api_0005(self):
        data = 'username=byhy&password=88888888&password=88888888'
        
        response = requests.post('http://127.0.0.1/api/mgr/signin',data=data.encode('utf8'),proxies={'http':'http://127.0.0.1:8888'})
        rs_code = response.status_code
        rs_body = response.json()
        rs_ret = rs_body['ret']
        if rs_ret:
            rs_msg = rs_body['msg']
        else:
            rs_msg = None
        print(f'rs_code:{rs_code},rs_ret:{rs_ret},rs_msg:{rs_msg}')
        assert rs_code == 200
        assert rs_ret == 1
        assert rs_msg == '参数格式错误'



class Test_api_0006:

    def test_api_0006(self):
        data = 'username=byhy&username=byhy&password=88888888'

        response = requests.post('http://127.0.0.1/api/mgr/signin',data=data.encode('utf8'),proxies={'http':'http://127.0.0.1:8888'})
        rs_code = response.status_code
        rs_body = response.json()
        rs_ret = rs_body['ret']
        if rs_ret:
            rs_msg = rs_body['msg']
        else:
            rs_msg = None
        print(f'rs_code:{rs_code},rs_ret:{rs_ret},rs_msg:{rs_msg}')
        
        allure.dynamic.feature('登陆函数里')
        allure.dynamic.story('登陆测试3333')
        assert rs_code == 200
        assert rs_ret == 1
        assert rs_msg == '参数格式错误'
    

    

